<template>
	<xn-form-container
		:title="formData.id ? '编辑项目' : '增加项目'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="项目名称：" name="project_name">
				<a-input placeholder="请输入项目名称" autocomplete="off" v-model:value="formData.project_name"></a-input>
			</a-form-item>
			<a-form-item label="项目地点：" name="project_position">
				<a-input placeholder="请输入项目地点" autocomplete="off" v-model:value="formData.project_position"></a-input>
			</a-form-item>
			<a-form-item label="项目时间：" name="project_time">
				<a-date-picker style="width: 100%" v-model:value="formData.project_time" placeholder="请选择项目时间" />
			</a-form-item>
			<a-form-item label="备注：" name="remark">
				<a-textarea :rows="4" placeholder="请输入备注" autocomplete="off" v-model:value="formData.remark"></a-textarea>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required, rules } from '@/utils/formRules'
	import smCrypto from '@/utils/smCrypto'
	import dayjs from 'dayjs'
	import api from './api'
	import userApi from '../../customerInfo/api'
	import { useGlobalStore } from '@/store'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		time: []
	})
	const globalStore = useGlobalStore()
	const name = globalStore.userInfo.user_name || '摸星'
	let index = 0
	// 打开抽屉
	const onOpen = (record, id) => {
		visible.value = true
		index = id
		formData.value = {
			persion_name: '',
			project_time: dayjs()
		}
		if (record) {
			formData.value = Object.assign({}, record, {
				project_time: dayjs(record.project_time)
			})
			console.log(formData.value)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}

	// 默认要校验的
	const formRules = {
		project_name: [required('请输入项目名称')],
		project_position: [required('请输入项目位置')],
		project_time: [required('请选择项目时间')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				api
					.submitForm(
						{
							...formData.value,
							...(formData.value.id
								? {
										create_time: dayjs(formData.value.create_time).format('YYYY-MM-DD HH:mm:ss'),
										project_time: dayjs(formData.value.project_time).format('YYYY-MM-DD HH:mm:ss')
								  }
								: {
										create_time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
										project_time: dayjs(formData.value.project_time).format('YYYY-MM-DD HH:mm:ss')
								  }),
							modified_time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
							persion_name: name
						},
						formData.value.id
					)
					.then(() => {
						onClose()
						emit('successful')
					})
			})
			.catch((error) => {
				console.log(error)
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
